﻿var allcontextmenus = new Array();
var currentMenu;

function contextMenu(props, events) {
    this.props = props;
    this.events = events;
    this.menuObj = document.getElementById(this.props.menuID);
    this.targetEle = this.props.targetEle;
    //this.eventFunc = this.events;
    allcontextmenus.push(this);
}

contextMenu.prototype.buildContextMenu = function() {
    //    var menuObj = document.getElementById(this.props.menuID);
    //    var targetEle = this.props.targetEle;
    var eventFunc = this.events;

    document.oncontextmenu = function(evt) {
        try {

            clearEventBubble(evt);
            var cobj = ele(evt);
            if (currentMenu == null || currentMenu.currentObj != cobj) {
                currentMenu = existsMenuClass(cobj.className);
            }
            if (currentMenu != null) {
                eventFunc = currentMenu.events;
                hide();
                if (currentMenu.currentObj != null) {
                    currentMenu.currentObj.className = currentMenu.targetEle;
                }
                currentMenu.currentObj = cobj;

                cobj.className = 'MouseOver';


                var _items = currentMenu.menuObj.getElementsByTagName("li");
                for (var i = 0; i < _items.length; i++) {
                    if (_items[i].className != "separator" && containsItem(_items[i].id)) {
                        _items[i].className = "item";
                        _items[i].onmouseout = function() { this.className = "item"; } // if (this.id == "edit") { this.getElementsByTagName('ul')[0].style.display = "none"; } };
                        _items[i].onclick = function() { hide(); func(this.id, cobj); };
                        _items[i].onmouseover = function() { this.className = "item itemOver"; } // if (this.id == "edit") { this.getElementsByTagName('ul')[0].style.display = "block"; } };
                        //modified by dingyuliang 2009-06-29
                        //                        if (_items[i].id == "edit") {
                        //                            var result = chatblog.usercate.chRoom.GetGroupListUlHtml(cobj.getElementsByTagName("input")[0].value, cobj.id);
                        //                            _items[i].getElementsByTagName("ul")[0].innerHTML = result.value;
                        //                        }
                    }
                }

                if (cobj.tagName == "H2" && (cobj.childNodes[0].nodeValue == "默认分组" || cobj.childNodes[0].nodeValue == "陌生人")) {
                    for (var i = 0; i < _items.length; i++)
                        if (_items[i].id == "renameGroup" || _items[i].id == "deleteGroup") {
                        _items[i].style.display = "none";
                    }
                }
                else {
                    for (var i = 0; i < _items.length; i++)
                        if (_items[i].id == "renameGroup" || _items[i].id == "deleteGroup") {
                        _items[i].style.display = "block";
                    }
                }

                var _bodyWidth = document.body.offsetWidth || document.body.clientWidth;
                var _bodyHeight = document.body.offsetHeight || document.body.clientHeight;
                var _mWidth = currentMenu.menuObj.style.width;
                var _mHeight = 120;
                currentMenu.menuObj.style.left = ((parseInt(getX(evt)) + parseInt(_mWidth)) > parseInt(_bodyWidth) ? (parseInt(getX(evt)) - parseInt(_mWidth)) : getX(evt)) + "px";
                currentMenu.menuObj.style.top = ((parseInt(getY(evt)) + parseInt(_mHeight)) > parseInt(_bodyHeight) ? (parseInt(getY(evt)) - parseInt(_mHeight) + 20) : getY(evt)) + "px";
                currentMenu.menuObj.style.display = "block";
                if (cobj.childNodes[0].nodeValue == "陌生人" && (parseInt(getY(evt)) + parseInt(_mHeight)) > parseInt(_bodyHeight)) {
                    currentMenu.menuObj.style.top = parseInt(getY(evt)) - 60 + "px";
                }

            } else {
                hide();
            }

        } catch (e) {
        } finally {
            clearEventBubble(evt);
            return false;
        }

    }

    document.onclick = function(thisObj) {
        hide();
        if (currentMenu != null && currentMenu.currentObj != null) { currentMenu.currentObj.className = currentMenu.targetEle; currentMenu.currentObj = null; }
        thisObj = thisObj || window.event;
        var thisObjEle = ele(thisObj);


    }

    func = function(fid, srcEle) {
        eventFunc.bindings[fid](srcEle);
    }

    hide = function() {
        try {
            for (i = 0; i < allcontextmenus.length; i++) {
                allcontextmenus[i].menuObj.style.display = "none";
                if (allcontextmenus[i].currentObj != null)
                    allcontextmenus[i].currentObj.className = allcontextmenus[i].targetEle;
            }
        } catch (e) { }
    }
    ele = function(evt) {
        evt = evt || window.event;
        return (evt.srcElement || evt.target);
    }



}

existsMenuClass = function(cssName) {
    for (i = 0; i < allcontextmenus.length; i++) {
        if (allcontextmenus[i].props.targetEle == cssName)
            return allcontextmenus[i];
    }
    return null;
}

containsItem = function(itemId) {
    var itemidList = ['addGroup', 'add', 'addUser', 'deleteGroup', 'renameGroup', 'del', 'edit', 'prop', 'rename'];
    for (var i = 0; i < itemidList.length; i++) {
        if (itemidList[i] == itemId)
            return true;
    }
    return false;
}
/*==============================================================*/
function isIE() { //ie? 
    if (window.navigator.userAgent.toLowerCase().indexOf("msie") >= 1)
        return true;
    else
        return false;
}

if (!isIE()) { //firefox innerText define
    HTMLElement.prototype.__defineGetter__("innerText",
    function() {
        var anyString = "";
        var childS = this.childNodes;
        for (var i = 0; i < childS.length; i++) {

            if (childS[i].nodeType == 1)
            //anyString += childS[i].tagName=="BR" ? "\n" : childS[i].innerText;
                anyString += childS[i].innerText;
            else if (childS[i].nodeType == 3)
                anyString += childS[i].nodeValue;
        }
        return anyString;
    }
    );


    HTMLElement.prototype.__defineSetter__("innerText",
    function(sText) {
        this.textContent = sText;


    }
    );

}

function getX(evt) {
    evt = evt || window.event;
    /**
    CssRain---增加代码. 8月20日 
    使右键菜单，在有滚动条的情况下，也能正常在点击出弹出。
    */
    var scrollPosX;
    if (typeof window.pageYOffset != 'undefined') {
        scrollPosX = window.pageXOffset;
    }
    else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
        scrollPosX = document.documentElement.scrollLeft;
    }
    else if (typeof document.body != 'undefined') {
        scrollPosX = document.body.scrollLeft;
    }

    return (evt.x || evt.clientX || evt.pageX) + scrollPosX;
}

function getY(evt) {
    evt = evt || window.event;
    /**
    CssRain---增加代码. 8月20日 
    使右键菜单，在有滚动条的情况下，也能正常在点击出弹出。
    */
    var scrollPosY;
    if (typeof window.pageYOffset != 'undefined') {
        scrollPosY = window.pageYOffset;
    }
    else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
        scrollPosY = document.documentElement.scrollTop;
    }
    else if (typeof document.body != 'undefined') {
        scrollPosY = document.body.scrollTop;
    }

    return (evt.y || evt.clientY || evt.pageY) + scrollPosY;
}

function clearEventBubble(evt) {
    evt = evt || window.event;

    if (evt.stopPropagation) {
        evt.stopPropagation();
    } else {
        evt.cancelBubble = true;
    }

    if (evt.preventDefault) {
        evt.preventDefault();
    } else {
        evt.returnValue = false;
    }

} 


//
//----设置Cookie------

function setCookie(name, value, domain) {
    var value = escape(value);
    var nameString = "Drag_" + name + "=" + value; //给cookie变量增加前缀 
    var extime = new Date();
    extime.setTime(extime.getTime() + 315360000);
    var expiryString = ";expires=" + extime.toGMTString();
    var domainString = "";
    var pathString = ";path=/";
    var setvalue = nameString + expiryString;
    document.cookie = setvalue;
}

//----设置Cookie 结束------

//---读取Cookie-------

function getcookie(name) {
    var CookieFound = false;
    var start = 0;
    var end = 0;
    var CookieString = document.cookie;
    var i = 0;
    name = "Drag_" + name; //增加前缀
    while (i <= CookieString.length) {
        start = i;
        end = start + name.length;
        if (CookieString.substring(start, end) == name) {
            CookieFound = true;
            break;
        }
        i++;
    }

    if (CookieFound) {
        start = end + 1;
        end = CookieString.indexOf(";", start);
        if (end < start)
            end = CookieString.length;
        var getvalue = CookieString.substring(start, end);
        return unescape(getvalue);
    }
    return "";
}

//---读取Cookie 结束-------

//检测是否禁用了cookie
function isEnableCookie() {
    try {
        var cookie_helper = new CookieHelper("");
        var test_cookie_name = "test_cookie_name";
        var test_cookie_value = "test_cookie_value";
        var test_cookie_value_ret = "";

        cookie_helper.writeCookie(test_cookie_name, test_cookie_value);
        test_cookie_value_ret = cookie_helper.getCookieValue(test_cookie_name);

        if (test_cookie_value_ret != null) {
            cookie_helper.removeCookie(test_cookie_name);
            return true;
        } else {
            return false;
        }
    } catch (error) {
        return false;
    }
}


var moveX = 0;
var moveY = 0;
var moveTop = 0;
var moveLeft = 0;
var moveable = false;
var docMouseMoveEvent = document.onmousemove;
var docMouseUpEvent = document.onmouseup;
addDivonmousedown = function() {
    //alert('dd');
    msgObj = this;
    var evt = getEvent();
    moveable = true;
    moveX = evt.clientX;
    moveY = evt.clientY;
    moveTop = parseInt(msgObj.style.top);
    moveLeft = parseInt(msgObj.style.left);
    document.onmousemove = function() {
        if (moveable) {
            var evt = getEvent();
            var x = moveLeft + evt.clientX - moveX;
            var y = moveTop + evt.clientY - moveY;
            if (x > 0 && (x + w < iWidth) && y > 0 && (y + h < iHeight)) {
                msgObj.style.left = x + "px";
                msgObj.style.top = y + "px";
            }
        }
    };
    document.onmouseup = function() {
        if (moveable) {
            document.onmousemove = docMouseMoveEvent;
            document.onmouseup = docMouseUpEvent;
            moveable = false;
            moveX = 0;
            moveY = 0;
            moveTop = 0;
            moveLeft = 0;
        }
    };
}

function getEvent() {
    return window.event || arguments.callee.caller.arguments[0];
}


var popup_dragging = false;
var popup_target;
var popup_mouseX;
var popup_mouseY;
var popup_mouseposX;
var popup_mouseposY;
var popup_oldfunction;
function popup_display(x) {
    var win = window.open();
    for (var i in x) win.document.write(i + ' = ' + x[i] + '<br>');
}
function popup_mousedown(e) {
    var ie = navigator.appName == "Microsoft Internet Explorer";
    if (ie && window.event.button != 1) return;
    if (!ie && e.button != 0) return;
    popup_dragging = true;
    popup_target = this['target'];
    popup_mouseX = ie ? window.event.clientX : e.clientX;
    popup_mouseY = ie ? window.event.clientY : e.clientY;
    if (ie)
        popup_oldfunction = document.onselectstart;
    else popup_oldfunction = document.onmousedown;
    if (ie)
        document.onselectstart = new Function("return false;");
    else document.onmousedown = new Function("return false;");
}
function popup_mousemove(e) {
    if (!popup_dragging) return;
    popup_target = drag_element['target'];
    var ie = navigator.appName == "Microsoft Internet Explorer";
    var element = document.getElementById(popup_target);
    var mouseX = ie ? window.event.clientX : e.clientX;
    var mouseY = ie ? window.event.clientY : e.clientY;
    element.style.left = (element.offsetLeft + mouseX - popup_mouseX) + 'px';
    element.style.top = (element.offsetTop + mouseY - popup_mouseY) + 'px';
    popup_mouseX = ie ? window.event.clientX : e.clientX;
    popup_mouseY = ie ? window.event.clientY : e.clientY;
}

function popup_mouseup(e) {
    if (!popup_dragging) return;
    popup_dragging = false;
    popup_target = drag_element['target'];
    var ie = navigator.appName == "Microsoft Internet Explorer";
    var element = document.getElementById(popup_target);
    if (ie)
        document.onselectstart = popup_oldfunction;
    else document.onmousedown = popup_oldfunction;
}
function popup_exit(e) {
    popup_target = drag_element['target'];
    var ie = navigator.appName == "Microsoft Internet Explorer";
    var element = document.getElementById(popup_target);
    popup_mouseup(e);
    element.style.display = "none";
}
function popup_show(popupId, dragId, exitId, top, left, ismask, maskid, bodyalpha, notallowdrag) {
    element = document.getElementById(popupId);
    drag_element = document.getElementById(dragId);
    exit_element = document.getElementById(exitId);
    element.style.position = "absolute";
    element.style.left = (left == null) ? ((document.documentElement.scrollLeft + popup_mouseposX - 10) + 'px') : left;
    element.style.top = (top == null) ? ((document.documentElement.scrollTop + popup_mouseposY - 10) + 'px') : top;
    drag_element['target'] = popupId;
    element.style.display = "block";
    if (ismask != null && ismask == true && maskid && bodyalpha) {
        maskBody(bodyalpha, maskid);
        exit_element.onclick = function(e) { e = e || window.event; popup_exit(e); remaskBody(maskid); }
        if (notallowdrag != true)
            drag_element.onmousedown = function(e) { e = e || window.event; popup_mousedown(e); }
    }
    else {
        if (notallowdrag != true)
            drag_element.onmousedown = popup_mousedown;
        exit_element.onclick = popup_exit;
    }
    //drag_element.onmousedown = popup_mousedown;
    //exit_element.onclick = popup_exit;
}
function popup_mousepos(e) {
    var ie = navigator.appName == "Microsoft Internet Explorer";
    popup_mouseposX = ie ? window.event.clientX : e.clientX;
    popup_mouseposY = ie ? window.event.clientY : e.clientY;
}
if (navigator.appName == "Microsoft Internet Explorer")
    document.attachEvent('onmousedown', popup_mousepos);
else document.addEventListener('mousedown', popup_mousepos, false);
if (navigator.appName == "Microsoft Internet Explorer")
    document.attachEvent('onmousemove', popup_mousemove);
else document.addEventListener('mousemove', popup_mousemove, false);
if (navigator.appName == "Microsoft Internet Explorer")
    document.attachEvent('onmouseup', popup_mouseup);
else document.addEventListener('mouseup', popup_mouseup, false);

//隐藏body,出现半透明
function maskBody(bodyalpha, maskid) {
    var maskeEle = $("#" + maskid)[0];
    maskeEle.className = 'maskStyle';
    maskeEle.style.visibility = "visible";
    maskeEle.style.width = document.body.scrollWidth + "px";
    maskeEle.style.height = document.body.scrollHeight + "px";
    if (Sys.ie) {
        //ie
        maskeEle.style.filter = "alpha(opacity = " + bodyalpha.toString() + ")";
    }
    if (Sys.firefox) {
        //ff
        maskeEle.style.MozOpacity = bodyalpha / 100;
    } if (Sys.chrome) {
        //chrome
        maskeEle.style.opacity = bodyalpha / 100;
    }
}
//显示
function remaskBody(maskid) {
    $("#" + maskid)[0].style.visibility = "hidden";
    //$("#" + drag_element['target'])[0].style.display = "none";
    $("#" + maskid)[0].style.width = "0px";
    $("#" + maskid)[0].style.height = "0px";
}

